<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Edit Fedora data stream
 */
class escape_setup_action_EditFedoraDataStream extends escape_setup_Action
{
	public function handleRequest()
	{
		$pid = $_REQUEST['pid'];
		$dsId = $_REQUEST['dsId'];
		$dsContent = $_REQUEST['dsContent'];
		
		$repository = new escape_repository_Repository($this->requestAttributes['repositoryConfig']);
		$repository->initialize();
		
		if(isset($_POST['edit']))
		{
			// fetch the datastream content
			$dsContent = $repository->fedora->getDatastreamDissemination($pid, $dsId)->stream;
			
			$viewName = 'FedoraDataStreamEditInput';
		}
		else if(isset($_POST['save']))
		{
			// update the datastream content
			$repository->fedora->modifyDatastreamByValueString($pid, $dsId, $dsContent);
			
			// fetch the datastream content
			$dsContent = $repository->fedora->getDatastreamDissemination($pid, $dsId)->stream;
			
			// try to update the indexes
			try
			{
				$objectUri = $repository->fedora->convertPidToUri($pid);
				$oreObject = $repository->getOreObjectByUri($objectUri);
				$oreObject->updateIndex();
			}
			catch(Exception $exception)
			{
			}
			
			$this->requestAttributes['updateComplete'] = true;
			
			$viewName = 'FedoraDataStreamEditInput';
		}
		else
		{
			$viewName = 'FedoraDataStreamEditSelect';
		}

		$repository->shutdown();

		$this->requestAttributes['pid'] = $pid;
		$this->requestAttributes['dsId'] = $dsId;
		$this->requestAttributes['dsContent'] = $dsContent;
		
		return $viewName;
	}
}